package com.csun.cmny.provider.service.impl;

import com.csun.cmny.base.dto.LoginAuthDto;
import com.csun.cmny.base.enums.ErrorCodeEnum;
import com.csun.cmny.core.support.BaseService;
import com.csun.cmny.provider.exceptions.OmcBizException;
import com.csun.cmny.provider.exceptions.OpcBizException;
import com.csun.cmny.provider.mapper.OpcFlowMessageGroupMapper;
import com.csun.cmny.provider.mapper.OpcFlowMessageMapper;
import com.csun.cmny.provider.model.domain.OpcFlowMessage;
import com.csun.cmny.provider.model.domain.OpcFlowMessageGroup;
import com.csun.cmny.provider.model.dto.message.FlowMessageQueryDto;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.OpcFlowMessageService;
import com.csun.cmny.util.PublicUtil;
import com.github.pagehelper.PageHelper;
import com.google.common.base.Preconditions;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2019-11-07 11:06
 * @Description:
 */
@Service
@Transactional
public class OpcFlowMessageServiceImpl extends BaseService<OpcFlowMessage> implements OpcFlowMessageService {

    @Resource
    private OpcFlowMessageMapper opcFlowMessageMapper;
    @Resource
    private OpcFlowMessageGroupMapper opcFlowMessageGroupMapper;
    @Resource
    private CmnyMessageSource cmnyMessageSource;

    /**
     * add omc flow message
     *
     * @param opcFlowMessage the omc flow message
     * @param loginAuthDto   the login auth dto
     *
     * @return the int
     */
    @Override
    public Integer add(OpcFlowMessage opcFlowMessage, LoginAuthDto loginAuthDto) {

        OpcFlowMessage omcFlowMessageQuery = new OpcFlowMessage();
        omcFlowMessageQuery.setCode(opcFlowMessage.getCode());
        int count = opcFlowMessageMapper.selectCount(omcFlowMessageQuery);
        if (count > 0) {
            throw new OmcBizException(ErrorCodeEnum.OMC10031401);
        }

        opcFlowMessage.setUpdateInfo(loginAuthDto);

        return opcFlowMessageMapper.insertSelective(opcFlowMessage);
    }

    /**
     * edit omc flow message
     *
     * @param opcFlowMessage the omc flow message
     * @param loginAuthDto   the login auth dto
     * @return the int
     */
    @Override
    public Integer edit(OpcFlowMessage opcFlowMessage, LoginAuthDto loginAuthDto) {

        // id 不能为空
        Preconditions.checkArgument(PublicUtil.isNotEmpty(opcFlowMessage.getId()),
                cmnyMessageSource.getMessage("flow.message.id.null", null));

        OpcFlowMessage omcFlowMessageQuery = opcFlowMessageMapper.selectByPrimaryKey(opcFlowMessage.getId());
        if (PublicUtil.isEmpty(omcFlowMessageQuery)) {
            //
            throw new OmcBizException(ErrorCodeEnum.OMC10031402);
        }

        // code 是否冲突
        if (PublicUtil.isNotEmpty(opcFlowMessage.getCode())) {
            omcFlowMessageQuery = new OpcFlowMessage();
            omcFlowMessageQuery.setCode(opcFlowMessage.getCode());
            omcFlowMessageQuery = opcFlowMessageMapper.selectOne(omcFlowMessageQuery);
            if (omcFlowMessageQuery != null && !omcFlowMessageQuery.getId().equals(opcFlowMessage.getId())) {
                throw new OmcBizException(ErrorCodeEnum.OMC10031401);
            }
        }

        opcFlowMessage.setUpdateInfo(loginAuthDto);

        return opcFlowMessageMapper.updateByPrimaryKeySelective(opcFlowMessage);
    }

    /**
     * delete by id
     *
     * @param id the omc flow message id
     * @return the int
     */
    @Override
    public Integer deleteById(Long id) {

        // 判断已经配置
        OpcFlowMessageGroup opcFlowMessageGroup = new OpcFlowMessageGroup();
        opcFlowMessageGroup.setFlowMessageId(id);
        int count = opcFlowMessageGroupMapper.selectCount(opcFlowMessageGroup);
        if (count > 0) {
            throw new OpcBizException(ErrorCodeEnum.OMC10031404);
        }

        return opcFlowMessageMapper.deleteByPrimaryKey(id);
    }

    /**
     * query omc flow message list with page
     *
     * @param flowMessageQueryDto the flow message query dto
     *
     * @return the list.
     */
    @Override
    public List<OpcFlowMessage> queryListWithPage(FlowMessageQueryDto flowMessageQueryDto) {

        PageHelper.startPage(flowMessageQueryDto.getPageNum(), flowMessageQueryDto.getPageSize());

        return opcFlowMessageMapper.queryListWithPage(flowMessageQueryDto);
    }

}
